package com.ltmall.api.comm;

import com.ltmall.api.util.PropertiesUtils;
import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import java.io.IOException;

public class HttpClientUtil {

    // private static final Log log = LogFactory .getLog(HttpClientUtil.class);

    /**
     * get方式
     *
     * @param order
     * @param id
     * @return
     */
    public static String getHttp(String order, String id) {
        String responseMsg = "";

        // 1.构造HttpClient的实例
        HttpClient httpClient = new HttpClient();

        String uid = PropertiesUtils.getProperties().getProperty("EXPRESS_UID");
        String key = PropertiesUtils.getProperties().getProperty("EXPRESS_KEY");

        String param1_uid = "uid=" + uid;
        String param_key = "&key=" + key;
        String param_order = "&order=" + order;
        String param_id = "&id=" + id;
        String url = "http://www.kuaidiapi.cn/rest/?" + param1_uid + param_key + param_order + param_id;

        // 2.创建GetMethod的实例
        GetMethod getMethod = new GetMethod(url);
        getMethod.addRequestHeader("Content-Type", "text/html; charset=UTF-8");

        // 使用系统系统的默认的恢复策略
        getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new DefaultHttpMethodRetryHandler());

        try {
            // 3.执行getMethod,调用http接口
            httpClient.executeMethod(getMethod);

            // 4.读取内容
            byte[] responseBody = getMethod.getResponseBody();

            // 5.处理返回的内容
            responseMsg = new String(responseBody, "utf-8");

        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 6.释放连接
            getMethod.releaseConnection();
        }
        return responseMsg;
    }


    /**
     * post方式
     *
     * @param param1
     * @param param2
     * @return
     */
    public static String postHttp(String param1, String param2) {
        String responseMsg = "";
        // 1.构造HttpClient的实例
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setContentCharset("GBK");
        String url = "http://localhost:8080/UpDown/httpServer";

        // 2.构造PostMethod的实例
        PostMethod postMethod = new PostMethod(url);

        // 3.把参数值放入到PostMethod对象中
        // 方式1：
        /*
         * NameValuePair[] data = { new NameValuePair("param1", param1), new
         * NameValuePair("param2", param2) }; postMethod.setRequestBody(data);
         */

        // 方式2：
        postMethod.addParameter("param1", param1);
        postMethod.addParameter("param2", param2);

        try {
            // 4.执行postMethod,调用http接口
            httpClient.executeMethod(postMethod);// 200
            // 5.读取内容
            responseMsg = postMethod.getResponseBodyAsString().trim();
            // log.info(responseMsg);
            // 6.处理返回的内容

        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 7.释放连接
            postMethod.releaseConnection();
        }
        return responseMsg;
    }


    //支付宝退款测试
    public void testZFB() {
        String appid = "wx46e1d4377e2b58e6";
        String mch_id = "1336857501";
        String nonce_str = "12345";
        String sign = "cvasdbvasbadcasdvasddcwcsdc";
        String out_trade_no = "NO201606261404593078005505";
        String out_refund_no = "24534578542";

        String total_fee = "1";
        String refund_fee = "1";
        String op_user_id = "354265354";


        zfbRefund(appid, mch_id, nonce_str, sign, out_trade_no, out_refund_no, total_fee, refund_fee, op_user_id);
    }

    //提交支付宝退款请求
    public static String zfbRefund(String appid, String mch_id, String nonce_str, String sign, String out_trade_no, String out_refund_no, String total_fee, String refund_fee, String op_user_id) {
        String responseMsg = "";
        // 1.构造HttpClient的实例
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setContentCharset("GBK");
        String url = "https://api.mch.weixin.qq.com/secapi/pay/refund";

        // 2.构造PostMethod的实例
        PostMethod postMethod = new PostMethod(url);

        // 3.把参数值放入到PostMethod对象中
        postMethod.addParameter("appid", appid);
        postMethod.addParameter("mch_id", mch_id);
        postMethod.addParameter("nonce_str", nonce_str);
        postMethod.addParameter("sign", sign);
        postMethod.addParameter("out_trade_no", out_trade_no);
        postMethod.addParameter("out_refund_no", out_refund_no);
        postMethod.addParameter("total_fee", total_fee);
        postMethod.addParameter("refund_fee", refund_fee);
        postMethod.addParameter("op_user_id", op_user_id);

        try {
            // 4.执行postMethod,调用http接口
            httpClient.executeMethod(postMethod);// 200
            // 5.读取内容
            responseMsg = postMethod.getResponseBodyAsString().trim();
            // log.info(responseMsg);
            // 6.处理返回的内容

        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 7.释放连接
            postMethod.releaseConnection();
        }
        return responseMsg;
    }

}
